package com.shijunhao.graduation.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shijunhao.graduation.domain.dto.BusinessForumDto;
import com.shijunhao.graduation.domain.entity.*;
import com.shijunhao.graduation.domain.vo.*;
import com.shijunhao.graduation.enums.TableName;
import com.shijunhao.graduation.exception.BusinessException;
import com.shijunhao.graduation.mapper.BusinessForumMapper;
import com.shijunhao.graduation.properties.RoleParams;
import com.shijunhao.graduation.service.business.*;
import com.shijunhao.graduation.service.scheduled.ForumTypeScheduleService;
import com.shijunhao.graduation.service.system.impl.BaseServiceImpl;
import com.shijunhao.graduation.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shijunhao
 * @since 2022-04-16
 */
@Service
public class BusinessForumServiceImpl extends BaseServiceImpl<BusinessForumMapper, BusinessForum> implements IBusinessForumService {

    @Resource
    private IBusinessHotInfoService businessHotInfoService;
    @Resource
    private IBusinessUserInfoService businessUserInfoService;
    @Resource
    private IBusinessForumThymeService businessForumThymeService;
    @Resource
    private IBusinessForumTypeService businessForumTypeService;
    @Resource
    private IBusinessForumDiscussService businessForumDiscussService;
    @Resource
    private ForumTypeScheduleService forumTypeScheduleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(BusinessForumDto dto) {
        BusinessForum entity = new BusinessForum();
        BeanUtils.copyProperties(dto, entity);
        this.insertOne(entity);
        businessHotInfoService.insertDefaultHotInfo(TableName.BUSINESS_FORUM.getLabel(), entity.getId());
        forumTypeScheduleService.updateOne(entity.getThyme());
        return entity.getId();
    }

    @Override
    public BusinessForumListVo toListVo(BusinessForum entity) {

        BusinessForumListVo result = new BusinessForumListVo();
        result.setHotInfo(businessHotInfoService.getHotInfo(TableName.BUSINESS_FORUM.getLabel(), entity.getId()));

        BusinessUserInfo userInfo = businessUserInfoService.getOne(Wrappers.lambdaQuery(BusinessUserInfo.class).eq(BusinessUserInfo::getUid, entity.getCreateBy()));
        result.setUserInfo(userInfo);

        BusinessForumSimpleVo simpleVo = new BusinessForumSimpleVo();
        simpleVo.setId(entity.getId());
        simpleVo.setTitle(entity.getTitle());

        BusinessForumThyme thyme = businessForumThymeService.getById(entity.getThyme());
        if (thyme != null) {
            simpleVo.setThymeId(thyme.getId());
            simpleVo.setThymeName(thyme.getName());
        }

        BusinessForumType type = businessForumTypeService.getById(entity.getForumType());
        if (type != null) {
            simpleVo.setTypeId(type.getId());
            simpleVo.setTypeName(type.getName());
        }

        int i = businessForumDiscussService.selectCount(Wrappers.lambdaQuery(BusinessForumDiscuss.class).eq(BusinessForumDiscuss::getFid, entity.getId()));
        simpleVo.setFloorCount(i);
        result.setSimpleVo(simpleVo);

        return result;
    }

    @Override
    public BusinessForumVo mapping(BusinessForum entity) {
        BusinessForumVo businessForumVo = new BusinessForumVo();
        //获取简表信息
        BusinessForumListVo simple = toListVo(entity);
        //获取用户详细信息
        businessForumVo.setUserInfo(simple.getUserInfo());
        businessForumVo.setForum(entity);
        //获取类型主题信息
        businessForumVo.setTypeVo(businessForumTypeService.toVo(businessForumTypeService.getById(entity.getForumType())));
        businessForumVo.setThymeVo(businessForumThymeService.toVo(businessForumThymeService.getById(entity.getThyme())));
        //获取热度信息
        businessForumVo.setHotInfo(simple.getHotInfo());
        //获取评论信息
        Page<DiscussVo> discuss = (Page<DiscussVo>) businessForumDiscussService.page(PageUtils.initPage(), entity.getId());
        businessForumVo.setDiscussVoPage(discuss);
        return businessForumVo;
    }

    @Override
    public IPage<BusinessForumMineVo> pageMine(IPage<BusinessForum> page) {
        CurrentUserVo user = businessUserInfoService.getCurrentOrgUser();
        Page<BusinessForum> result;
        if (RoleParams.ADMIN.equals(user.getRole())) {
            result = this.page(page, new QueryWrapper<>());
        } else {
            result = this.page(page, Wrappers.lambdaQuery(BusinessForum.class)
                    .eq(BaseEntity::getCreateBy, user.getUser().getId()));
        }
        return result.convert(this::toMineListVo);
    }

    @Override
    public BusinessForumMineVo toMineListVo(BusinessForum entity) {
        BusinessForumMineVo result = new BusinessForumMineVo();
        result.setListVo(this.toListVo(entity));
        result.setForum(entity);
        //获取评论数
        int count = businessForumDiscussService.selectCount(Wrappers.lambdaQuery(BusinessForumDiscuss.class)
                .eq(BusinessForumDiscuss::getFid, entity.getId()));
        result.setDiscussCount(count);
        return result;
    }

    @Override
    public boolean delete(Long id) {
        //判断是否是管理员或该评论的文章或论坛作者是否为本人
        CurrentUserVo user = businessUserInfoService.getCurrentOrgUser();
        BusinessForum forum = getById(id);
        if(!forum.getCreateBy().equals(user.getUser().getId()) && !RoleParams.ADMIN.equals(user.getRole())){
            throw new BusinessException("无删除权限");
        }else{
            return super.deleteById(id);
        }
    }

    @Override
    public boolean update(BusinessForumDto dto) {
        //判断是否是管理员或该评论的文章或论坛作者是否为本人
        CurrentUserVo user = businessUserInfoService.getCurrentOrgUser();
        BusinessForum forum = getById(dto.getId());
        if(!forum.getCreateBy().equals(user.getUser().getId()) && !RoleParams.ADMIN.equals(user.getRole())){
            throw new BusinessException("无修改权限");
        }else{
            //拷贝其他信息
            BeanUtils.copyProperties(dto,forum);
            return super.updateById(forum);
        }
    }
}
